package com.hzcx.app.simplechat.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.hzcx.app.simplechat.common.Common;
import com.hzcx.app.simplechat.ui.publicui.event.WxPaySuccessEvent;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Common.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        switch (baseResp.errCode){
            case 0:// 支付成功
                EventBus.getDefault().post(new WxPaySuccessEvent(true,"支付成功"));
                break;

            case -1: // 支付失败
                EventBus.getDefault().post(new WxPaySuccessEvent(false,"支付失败："+baseResp.errStr));
                break;

            case -2: // 取消支付
                EventBus.getDefault().post(new WxPaySuccessEvent(false,"取消支付"));
                break;
        }
        finish();
    }
}
